/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef CDEFS_H
#define CDEFS_H

/*PRQA S 0602,0603  EOF*/
#ifndef ROUNDUP
#define ROUNDUP(a, b) ((uint32)(((uint32)(a) + (uint32)((uint32)(b)-1)) & ((uint32)~((uint32)(b)-1))))
#endif

#define ROUNDDOWN(a, b) ((a) & ~((b)-1))

#define IS_ALIGNED(a, b) (!(((unsigned long)(a)) & (((unsigned long)(b))-1)))

#define _MIN(a, b) (((a) < (b)) ? (a) : (b))
#define _MAX(a, b) (((a) > (b)) ? (a) : (b))

#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))

#define _offsetof(type, member) (uint32)&(((type*)0)->member)

#define containerof(ptr, type, member) \
    ((type *)((unsigned long)(ptr) - _offsetof(type, member)))


#define ARRAY_SZ(x) (sizeof(x)/sizeof(x[0]))

typedef void (*fv_v) (void);

#if defined(__ICCARM__)
#define _SECTION(x)  @ #x
#define _USED   __root
#else
#define _SECTION(x)  __attribute__((section(#x)))
#define _USED    __attribute__((used))
#endif

#endif /* CDEFS_H */
